from apscheduler.schedulers.background import BackgroundScheduler
from django.db import transaction
from django.shortcuts import render
from django_apscheduler.jobstores import DjangoJobStore
from rest_framework import status
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from django.core.mail import send_mail
from django.conf import settings
from rest_framework_jwt.authentication import JSONWebTokenAuthentication

from common import CommonResult
from book.views import scheduler
from users.models import User
from users.schedule import send_notify_email
from users import serializers
from rest_framework.generics import GenericAPIView
from rest_framework.permissions import IsAdminUser, IsAuthenticated
from book.models import Book
from utils import MyPageNumberPagination
import datetime
import os


# 获取所有员工信息
class UserView(ListAPIView):
    serializer_class = serializers.UserViewSerializer
    queryset = User.objects.filter(is_staff=0).all()


# 普通用户修改基本信息和密码
class CommonUserView(APIView):
    # permission_classes = [IsAuthenticated]

    # 获取个人信息
    def get(self, requset):
        data_dic = requset.query_params
        user = User.objects.get(username=data_dic['username'])
        serializer = serializers.CommonUserViewSerializer(instance=user)
        return Response(serializer.data, status=status.HTTP_200_OK)

    # 修改个人信息
    def put(self, request):
        data_dic = request.data
        user = User.objects.get(username=data_dic['username'])
        serializer = serializers.UserViewSerializer(instance=user, data=data_dic, partial=True)
        serializer.is_valid()
        return Response({'code': 200, 'Msg': '修改信息成功'})

    # 修改个人密码
    def post(self, request):
        data_dic = request.data
        user = User.objects.get(username=data_dic['username'])
        if not user.check_password(data_dic['oldPassword']):
            return CommonResult.result(405, '原密码错误')
        else:
            user.set_password(data_dic['newPassword'])
            user.save()
            return CommonResult.ok('密码修改成功')


class AdminUserView(GenericAPIView):
    serializer_class = serializers.UserViewSerializer
    queryset = User.objects.all()

    # permission_classes = [IsAdminUser]

    def get(self, request):
        users = self.get_queryset()
        # page = MyPageNumberPagination()
        # page_roles = page.paginate_queryset(queryset=self.queryset, request=request, view=self)
        serializer = serializers.CommonUserViewSerializer(instance=users, many=True)
        return CommonResult.ok('查询成功', serializer.data)

    def post(self, request):
        user_data = request.data
        user_exist = User.objects.filter(username=user_data['username'])
        if user_exist.exists():
            return CommonResult.params_error('用户名重复')
        serializer = self.get_serializer(data=user_data,partial=True)
        serializer.is_valid()
        serializer.save()
        return CommonResult.ok('新建成功')

    @transaction.atomic()
    def delete(self, request, uname):
        user = User.objects.filter(username=uname)
        books = Book.objects.filter(u_id=uname)
        for book in books:
            if book.b_status == 0:
                return CommonResult.params_error("此用户存在进行中订单,如需删除请先结束订单")
        books.delete()
        user.delete()
        return CommonResult.ok('删除成功')

    def put(self, request, uname):
        user = User.objects.get(username=uname)
        user.set_password('123456')
        user.save()
        return CommonResult.ok('重置成功')
